<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-02-24 15:03:41
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_yongjin extends discuz_table
{
    public function __construct()
    {
        $this->_table         = 'jamesonread_yongjin';
        $this->_pk            = 'id';
        $this->_pre_cache_key = 'jamesonreadyongjin_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    public function fetch_all_bypage($start, $size)
    {
        $cacheid = 'fetch_all_bypage-'.$book_id.'-'.$start.'-'.$size;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = (array) DB::fetch_all('SELECT y.*,c.username,b.book_name,b.author FROM %t as y INNER JOIN %t as c ON y.buy_id=c.uid INNER JOIN %t as b ON y.book_id=b.book_id ORDER BY y.addtime DESC LIMIT %d,%d', array($this->_table, 'common_member', 'jamesonread_books', $start, $size));
        $this->store_cache($cacheid, $data);
        return $data;
    }
    public function count_e_byauthorid($author_id,$type)
    {
        // type=1累计销售金额，type=2累计打赏金额
        # code...
        $data = array();
        $data['jine'] = (int) DB::result_first('SELECT SUM(saleprice) FROM %t WHERE author_id=%d AND type=%d',array($this->_table,$author_id,$type));
        $data['yongjin'] = (int) DB::result_first('SELECT SUM(yongjin) FROM %t WHERE author_id=%d AND type=%d',array($this->_table,$author_id,$type));
        $data['shiji']= $data['jine']-$data['yongjin'];
        return $data;
    }
    public function count_e_bybook($book_id,$type)
    {
        // type=1累计销售金额，type=2累计打赏金额
        # code...
        $data = array();
        $data['jine'] = (int) DB::result_first('SELECT SUM(saleprice) FROM %t WHERE book_id=%d AND type=%d',array($this->_table,$book_id,$type));
        $data['yongjin'] = (int) DB::result_first('SELECT SUM(yongjin) FROM %t WHERE book_id=%d AND type=%d',array($this->_table,$book_id,$type));
        $data['shiji']= $data['jine']-$data['yongjin'];
        return $data;
    }
}